package com.hx.xiao.controller;


import com.hx.xiao.entity.Hou;
import com.hx.xiao.service.IHouService;
import com.hx.xiao.utils.Code;
import com.hx.xiao.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hx
 * @since 2024-03-21
 */
@RestController
@RequestMapping("/piyaoh/hou")
public class HouController {

    @Autowired
    private IHouService iHouService;

    @GetMapping("/addTop/{id}")
    public R addTop(@PathVariable Integer id){
        int i = iHouService.insertL(id);
        if (i>0){
            return new R(Code.UPDATE_OK,"修改成功");
        }else {
            return new R(Code.UPDATE_ERRO,"修改失败");
        }
    }

    @GetMapping("/findAll")
    public R findAllText(String komg){
        List<Hou> hous = iHouService.fingAllText(komg);
        if (hous!=null){
            return new R(Code.GET_OK,hous,"查询成功");
        }else {
            return new R(Code.GET_ERRO,"查询失败");
        }
    }

    @PostMapping("/byall")
    public R byAll(@RequestBody Hou hou){
        List<Hou> hous = iHouService.byAll(hou);
        if (hous!=null){
            return new R(Code.GET_OK,hous,"查询成功");
        }else {
            return new R(Code.GET_ERRO,"查询失败");
        }
    }

    @GetMapping("/findByid/{id}")
    public R findByid(@PathVariable Integer id){
        Hou hou = iHouService.findByid(id);
        if (hou!=null){
            return new R(Code.GET_OK,hou,"查询成功");
        }else {
            return new R(Code.GET_ERRO,"查询失败");
        }
    }



    @GetMapping("/suhistory/{id}")
    public R suhistory(@PathVariable Integer id){
        List<Hou> hous = iHouService.fingAllHistory(id);
        if (hous!=null){
            return new R(Code.GET_OK,hous,"查询成功");
        }else {
            return new R(Code.GET_ERRO,"查询失败");
        }
    }


    @GetMapping("/sucollect/{id}")
    public R sucollect(@PathVariable Integer id){
        List<Hou> hous = iHouService.fingAllCollect(id);
        if (hous!=null){
            return new R(Code.GET_OK,hous,"查询成功");
        }else {
            return new R(Code.GET_ERRO,"查询失败");
        }

    }

    @PostMapping("/biao")
    public R biao(String biaoti){
        List<Hou> hous = iHouService.fingName(biaoti);
        if (hous!=null){
            return new R(Code.GET_OK,hous,"查询成功");
        }else {
            return  new R(Code.GET_ERRO,"查询失败");
        }
    }

    @GetMapping("/kome/{komg}")
    public R kome(@PathVariable String komg){
        List<Hou> allKome = iHouService.findAllKome(komg);
        if (allKome!=null){
            return  new R(Code.GET_OK,allKome,"查询成功");
        }else {
            return  new R(Code.GET_ERRO,"查询失败");
        }
    }

    @PostMapping("/up")
    private R updateB(@RequestBody Hou hou){
        int i = iHouService.updateB(hou);
        if (i>0){
            return new R(Code.UPDATE_OK,"修改成功");
        }else {
            return new R(Code.UPDATE_ERRO,"修改失败");
        }
    }


    @GetMapping("/de/{id}")
    private R deleB(@PathVariable Integer id){
        int i = iHouService.deteleB(id);
        if(i>0){
            return new R(Code.DELETE_OK,"删除成功");
        }else {
            return new R(Code.DELETE_ERRO,"删除失败");
        }
    }

    @PostMapping("/add")
    private R addB(@RequestBody Hou hou){
        int i = iHouService.insertB(hou);
        if (i>0){
            return new R(Code.GET_OK,hou,"添加成功");
        }else {
            return new R(Code.GET_ERRO,"添加失败");
        }
    }
}
